package com.huawei.hms.framework.network.restclient.hwhttp.dns.dnstask;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsUtil;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.DnsCallable;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

public class TaskManager {
    private static final int PoolSize = 10;
    private static final String TAG = "TaskManager";
    private static volatile TaskManager instance;
    private ExecutorService lazyUpdateThreadPool;
    private ConcurrentHashMap<String, Future> loaclCallable = new ConcurrentHashMap();
    private final Object lock = new Object();
    private ExecutorService lookupThreadPool;
    private ConcurrentHashMap<String, Future> runningCallable = new ConcurrentHashMap();

    class LocalCallable implements Callable {
        private String hostname;

        public LocalCallable(String str) {
            this.hostname = str;
        }

        public List<InetAddress> call() {
            Throwable e;
            Logger.v(TaskManager.TAG, "localCallable call");
            ArrayList arrayList = new ArrayList();
            try {
                DNManager.getInstance().setResolverSource(this.hostname, 4);
                return Arrays.asList(InetAddress.getAllByName(this.hostname));
            } catch (UnknownHostException e2) {
                Logger.w(TaskManager.TAG, "dns failed from local, domain is:" + this.hostname);
                return arrayList;
            } catch (IllegalArgumentException e3) {
                e = e3;
                Logger.w(TaskManager.TAG, "dns failed from local, Exception: ", e);
                return arrayList;
            } catch (NullPointerException e4) {
                e = e4;
                Logger.w(TaskManager.TAG, "dns failed from local, Exception: ", e);
                return arrayList;
            }
        }
    }

    public static TaskManager getInstance() {
        if (instance == null) {
            synchronized (TaskManager.class) {
                if (instance == null) {
                    instance = new TaskManager();
                }
            }
        }
        return instance;
    }

    private TaskManager() {
        Logger.i(TAG, "DNS ThreadPool init!");
        this.lazyUpdateThreadPool = Executors.newFixedThreadPool(10);
        this.lookupThreadPool = Executors.newCachedThreadPool();
    }

    public void enqueue(Runnable runnable) {
        try {
            this.lazyUpdateThreadPool.execute(runnable);
        } catch (RejectedExecutionException e) {
            Logger.w(TAG, "the runnable task cannot be accepted for execution");
        }
    }

    public DomainResult lookup(String str) throws UnknownHostException {
        DomainResult domainResult;
        Logger.i(TAG, "lookup: " + str);
        synchronized (this.lock) {
            Future future = (Future) this.runningCallable.get(str);
            if (future == null) {
                Logger.i(TAG, "future == null");
                future = this.lookupThreadPool.submit(new DnsCallable(str, this.lookupThreadPool));
                this.runningCallable.put(str, future);
            }
            Logger.i(TAG, "future = " + future);
        }
        try {
            domainResult = (DomainResult) future.get();
        } catch (Throwable e) {
            Logger.w(TAG, "query failed DNS_TIMEOUT", e);
            domainResult = null;
        }
        this.runningCallable.remove(str);
        if (!DnsUtil.isIpListEmpty(domainResult)) {
            return domainResult;
        }
        Logger.w(TAG, "dns failed from local and dnkeeper, domain is :" + str);
        throw new UnknownHostException("dns failed from local and dnkeeper, domain is :" + str);
    }

    public List<InetAddress> localDnslookup(String str) throws UnknownHostException {
        ArrayList arrayList = new ArrayList();
        synchronized (this.lock) {
            Future future = (Future) this.loaclCallable.get(str);
            if (future == null) {
                Logger.i(TAG, "future == null");
                future = this.lookupThreadPool.submit(new LocalCallable(str));
                this.loaclCallable.put(str, future);
            }
            Logger.i(TAG, "future = " + future);
        }
        try {
            List<InetAddress> list = (List) future.get((long) DnsUtil.getDnstime(), TimeUnit.MILLISECONDS);
        } catch (Throwable e) {
            Logger.w(TAG, "query failed CANCEL_TIMEOUT", e);
            Object obj = arrayList;
        }
        this.loaclCallable.remove(str);
        if (list != null && !list.isEmpty()) {
            return list;
        }
        Logger.w(TAG, "dns failed from local, domain is :" + str);
        throw new UnknownHostException("dns failed from local, domain is :" + str);
    }
}
